package com.zero.youxiao.service;

import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.mms.model.UserAccount;
import com.zero.youxiao.dao.UserAccountDao;

@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserService {

	@Inject
	private UserAccountDao userAccountDao;

	@Transactional()
	public void registProc() throws SmsRuntimeException {
		UserAccount userAccount = new UserAccount();
		userAccount.setUserName("youxiao");
		userAccount.setUserPass("123");
		try {
			userAccountDao.add(userAccount);
		} catch (Exception e) {
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}

	@Transactional()
	public void login(UserAccount loginAccount) throws SmsRuntimeException {
		try {
			System.out.println(loginAccount.getUserName());
			UserAccount userAccount = userAccountDao.findByName(loginAccount
					.getUserName());
			if (userAccount == null)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.AUTH_USER_NOT_EXISTS, "账号不存在");
			if (!loginAccount.getUserPass().equals(userAccount.getUserPass()))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.AUTH_BAD_ACCESS, "用户密码不正确");
		} catch (SmsRuntimeException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
	
}
